﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HCMServices.DTO;
using System.Collections.Generic;
using System.Collections;
using HCMServices.BUS;
using HCMServices.DAO;

namespace HCMServices.BUS
{
    /// <summary>
    /// Engine support for searching services
    /// </summary>
    public class SearchEngine
    {
        ////public List<HardDataDTO> Search(string input, int serviceID)
        ////{
        ////    List<HardDataDTO> rows = new List<HardDataDTO>();
        ////   string str = SearchSearchUtilities.FormatInputString(input);
        ////    // TODO: Code below

        ////    return rows;
        ////}
        private string connectString = "Data Source = isostore:/Database.sdf";
        private AutoComplete _autoCompleteEngine = new AutoComplete();
        private const int _FILTER_BY_BOTH = 0;
        private const int _FILTER_BY_DUONG_ONLY = 1;
        private const int _FILTER_BY_DICHVU_ONLY = 2;
        private const int _FILTER_BY_NONE = 3;

        public static int _FILTER_INFO_DUONG = 0;
        public static int _FILTER_INFO_DICHVU = 1;
        public static int _FILTER_INFO_GIA = 2;
        private const string _TAT_CA_DUONG = "Tất cả đường";
        private const string _TAT_CA_DICHVU = "Tất cả dịch vụ";
        private const string _TAT_CA_GIA = "Tất cả giá"; 

        private ServiceDAO _serviceDAO = null;
        private DataDAO _duLieuDAO = null;
        private StreetDAO _streetDAO = null;
        private WardDAO _wardDAO = null;
        private DistrictDAO _districtDAO = null;
        private PlaceNameDAO _placeNameDAO = null;
        private CityDAO _cityDAO = null;
        private KeyPlaceNameDAO _tuKhoaDiaDiemDAO = null;
        private KeyServiceDAO _tuKhoaDichVuDAO = null;
        private KeyStreetDAO _tuKhoaDuongDAO = null;
        private KeyWardDAO _tuKhoaPhuongDAO = null;
        private KeyDistrictDAO _tuKhoaQuanHuyenDAO = null; 
        private KeyCityDAO _tuKhoaTinhThanhDAO = null;

        private DetailServiceDAO _DetailServiceDAO;
        private DetailDataDAO _DetailDataDAO;
        private List<DetailServiceDTO> _ListDetailServiceDTO = new List<DetailServiceDTO>();

        public List<DetailServiceDTO> ListDetailServiceDTO
        {
            get { return _ListDetailServiceDTO; }
            set { _ListDetailServiceDTO = value; }
        }

        private List<SearchResultObject> _lstSearchResult = null; 

        // danh cho filter - danh sach DUONG, PHUONG, QUANHUYEN tren bo nho
        private List<StreetDTO> _listAllStreet = new List<StreetDTO>();
        private List<ServiceDTO> _listAllService = new List<ServiceDTO>();
        private List<DistrictDTO> _listAllDistrict = new List<DistrictDTO>();
        private List<PlaceNameDTO> _listAllPlaceName = new List<PlaceNameDTO>();
        private List<WardDTO> _listAllWard = new List<WardDTO>();
        private List<CityDTO> _listAllCity = new List<CityDTO>();

        private List<string> _listResultService = new List<string>(); 
        private List<string> _listResultStreet = new List<string>();
        private List<string> _Suggest = new List<string>();

        private Dictionary<String, String> _DictionaryCity = new Dictionary<String, String>();
        private Dictionary<String, String> _DictionaryDistrict = new Dictionary<String, String>();
        private Dictionary<String, String> _DictionaryPlaceName = new Dictionary<String, String>();
        private Dictionary<String, String> _DictionaryService = new Dictionary<String, String>();
        private Dictionary<String, String> _DictionaryStreet = new Dictionary<String, String>();
        private Dictionary<String, String> _DictionaryWard = new Dictionary<String, String>();

        /// <summary>
        /// Get _listResultStreet
        /// </summary>
        public List<string> ListResultStreet
        {
            get { return _listResultStreet; }
        }

        /// <summary>
        /// Get _listResultService
        /// </summary>
        public List<string> ListResultService
        {
            get { return _listResultService; }
        }

        /// <summary>
        /// Constructor of class
        /// </summary>
        public SearchEngine()
        {
            _serviceDAO = new ServiceDAO(connectString);
            _duLieuDAO = new DataDAO(connectString);
            _streetDAO = new StreetDAO(connectString);
            _wardDAO = new WardDAO(connectString);
            _districtDAO = new DistrictDAO(connectString);
            _placeNameDAO = new PlaceNameDAO(connectString);
            _cityDAO = new CityDAO(connectString);
            _tuKhoaDiaDiemDAO = new KeyPlaceNameDAO(connectString);
            _tuKhoaDichVuDAO = new KeyServiceDAO(connectString);
            _tuKhoaDuongDAO = new KeyStreetDAO(connectString);
            _tuKhoaPhuongDAO = new KeyWardDAO(connectString);
            _tuKhoaQuanHuyenDAO = new KeyDistrictDAO(connectString);
            _tuKhoaTinhThanhDAO = new KeyCityDAO(connectString);


            _DetailServiceDAO = new DetailServiceDAO(connectString);
            _DetailDataDAO = new DetailDataDAO(connectString);

            _DictionaryCity = _tuKhoaTinhThanhDAO.GetDictionaryOfKey();
            _DictionaryDistrict = _tuKhoaQuanHuyenDAO.GetDictionaryOfKey();
            _DictionaryPlaceName = _tuKhoaDiaDiemDAO.GetDictionaryOfKey();
            _DictionaryService = _tuKhoaDichVuDAO.GetDictionaryOfKey();
            _DictionaryStreet = _tuKhoaDuongDAO.GetDictionaryOfKey();
            _DictionaryWard = _tuKhoaPhuongDAO.GetDictionaryOfKey();



            this._listAllStreet = this._streetDAO.GetAllDuongDTO();
            this._listAllService = this._serviceDAO.GetAllDichVuDTO();
            _listAllDistrict = _districtDAO.GetAllQuanHuyenDTO();
            _listAllPlaceName = this._placeNameDAO.GetAllPlaceNameDTO();
            _listAllWard = _wardDAO.GetAllPhuongDTO();
            _listAllCity = _cityDAO.GetAllCityDTO();


           

        }

        /// <summary>
        /// Search normal
        /// </summary>
        /// <param name="inputString">String user want to find</param>
        /// <returns>List of DataDTO object </returns>
        public List<DataDTO> SearchForDataDTOResult(string inputString)
        {
            List<DataDTO> result = new List<DataDTO>();

            //List<HardDataDTO> rowsResult = new List<HardDataDTO>();

            //string standarString = SearchUtilities.FormatInputData(inputString);

            int maDichVu = 0;

            //if (suggest.Count > 0)
            //    result = this.Search(suggest[0], maDichVu);
            //else
            result = this.Search(inputString, maDichVu);

            return result;
        }

        /// <summary>
        /// Search with filter
        /// </summary>
        /// <param name="inputString">String input by user</param>
        /// <returns>List of HardDataDTO object</returns>
        //public List<HardDataDTO> SearchForHardDataDTOResult(string inputString)
        //{
        //    List<DataDTO> result = new List<DataDTO>();

        //    List<HardDataDTO> rowsResult = new List<HardDataDTO>();

        //    string standarString = SearchUtilities.FormatInputData(inputString);

        //    List<string> suggest = this._autoCompleteEngine.Suggest(inputString);

        //    int maDichVu = 0;

        //    //result = this.Search(inputString, maDichVu);
            

        //    if (suggest.Count > 0)
        //        result = this.Search(suggest[0], maDichVu);
        //    else
        //        result = this.Search(standarString, maDichVu);

        //    _listResultStreet = GetFilterInfo(result, _FILTER_INFO_DUONG);
        //    _listResultService = GetFilterInfo(result, _FILTER_INFO_DICHVU);


        //    rowsResult = this.ConvertToHardData(result);

        //    return rowsResult;
        //}

        public List<string> GetSuggest(string input)
        {
            List<string> suggest = this._autoCompleteEngine.Suggest(input);

            return suggest;
        }


        public List<HardDataDTO> ReturnHardDataFromDataDTO(List<DataDTO> input)
        {
            List<HardDataDTO> rowsResult = new List<HardDataDTO>();
            rowsResult = this.ConvertToHardData(input);
            return rowsResult;
        }

        public DetailServiceDTO GetDetaiServiceDTO(DataDTO input)
        {
            List<DetailDataDTO> detailData = this._DetailDataDAO.Seach(input.DataId);
            if(detailData.Count > 0)
                return this._DetailServiceDAO.Search(detailData[0].ServiceID);
            return null;
        }

        public List<DetailServiceDTO> GetListDetaiServiceDTO(List<DataDTO> input)
        {
            List<DetailServiceDTO> result = new List<DetailServiceDTO>();
            for (int i = 0; i < input.Count; i++)
            {
                result.Add(GetDetaiServiceDTO(input[i]));
            }
            return result;
        }

        public List<DetailServiceDTO> GetListDetaiServiceDTO(DataDTO input)
        {
            List<DetailDataDTO> detailData = this._DetailDataDAO.Seach(input.DataId);
            List<DetailServiceDTO> detailService = new List<DetailServiceDTO>();
            for (int i = 0; i < detailData.Count; i++)
            {
                detailService.Add(this._DetailServiceDAO.Search(detailData[i].ServiceID));
            }
            return detailService;
        }



        private List<DataDTO> Search(string strInput, int maDichVu)
        {
            
            List<DataDTO> lstResult = new List<DataDTO>();

            // xoa ket qua cu
            if (null != this._lstSearchResult)
            {
                this._lstSearchResult.Clear();
            }
            else
            {
                this._lstSearchResult = new List<SearchResultObject>();
            }

            // tim kiem
            strInput = SearchUtilities.FormatInputData(strInput);
            if (strInput.Length > 0)
            {
                bool isExistNumber = SearchUtilities.CheckExistNumber(strInput);
                if (isExistNumber)
                {
                    List<DataDTO> lstDataDTO = this._duLieuDAO.SearchWithSoNha(strInput);

                    List<DataDTO> temp = lstDataDTO;
                    lstDataDTO = RemoveDupicatesOnSoNha(temp);

                    int nDataDTO = lstDataDTO.Count;
                    if (nDataDTO > 0)
                    {
                        string strTemp = string.Empty;
                        for (int i = 0; i < nDataDTO; i++)
                        {
                            strTemp = Clear(strInput, lstDataDTO[i].NumberOfHome);
                            if (strTemp.Length > 0)
                            {
                                this.SearchTuKhoaDichVu(strTemp, lstDataDTO[i].NumberOfHome, maDichVu);
                            }
                            else
                            {
                                SearchResultObject sr = new SearchResultObject();
                                sr.SoNha = lstDataDTO[i].NumberOfHome;
                                if (0 != maDichVu)
                                {
                                    sr.MaDichVu = maDichVu;
                                }

                                this._lstSearchResult.Add(sr);
                            }
                        }
                    }

                    this.SearchTuKhoaDichVu(strInput, string.Empty, maDichVu);
                }
                else
                {
                    this.SearchTuKhoaDichVu(strInput, string.Empty, maDichVu);
                }

                List<DataDTO> lstTemp = null;
                int nSearchResult = this._lstSearchResult.Count;
                for (int i = 0; i < nSearchResult; i++)
                {
                    lstTemp = this._duLieuDAO.Search(
                        this._lstSearchResult[i].SoNha,
                        this._lstSearchResult[i].MaDichVu,
                        this._lstSearchResult[i].MaTinhThanh,
                        this._lstSearchResult[i].MaQuanHuyen,
                        this._lstSearchResult[i].MaPhuong,
                        this._lstSearchResult[i].MaDuong,
                        this._lstSearchResult[i].MaTenDiaDiem);
                    lstResult.AddRange(lstTemp);
                }
            }
            else
            {
                if (0 != maDichVu)
                {
                    this.SearchTuKhoaDichVu(strInput, string.Empty, maDichVu);
                    List<DataDTO> lstTemp = null;
                    int nSearchResult = this._lstSearchResult.Count;
                    for (int i = 0; i < nSearchResult; i++)
                    {
                        lstTemp = this._duLieuDAO.Search(
                            this._lstSearchResult[i].SoNha,
                            this._lstSearchResult[i].MaDichVu,
                            this._lstSearchResult[i].MaTinhThanh,
                            this._lstSearchResult[i].MaQuanHuyen,
                            this._lstSearchResult[i].MaPhuong,
                            this._lstSearchResult[i].MaDuong,
                            this._lstSearchResult[i].MaTenDiaDiem);
                        lstResult.AddRange(lstTemp);
                    }
                }
            }

            return lstResult;
        }

        private void SearchTuKhoaDichVu(string strInput, string soNha, int maDichVu)
        {
            if (0 == maDichVu) // tim tat ca dich vu
            {
                List<KeyServiceDTO> lstTuKhoaDichVuDTO = this._tuKhoaDichVuDAO.Search(strInput);
                int nTuKhoaDichVuDTO = lstTuKhoaDichVuDTO.Count;
                if (nTuKhoaDichVuDTO > 0)
                {
                    string strTemp = string.Empty;
                    for (int i = 0; i < nTuKhoaDichVuDTO; i++)
                    {
                        strTemp = Clear(strInput, lstTuKhoaDichVuDTO[i].KeyService);
                        if (strTemp.Length > 0)
                        {
                            this.SearchTuKhoaTinhThanh(strTemp, soNha, lstTuKhoaDichVuDTO[i].ServiceId);
                        }
                        else
                        {
                            SearchResultObject sr = new SearchResultObject();
                            sr.SoNha = soNha;
                            sr.MaDichVu = lstTuKhoaDichVuDTO[i].ServiceId;
                            this._lstSearchResult.Add(sr);
                        }
                    }
                }

                this.SearchTuKhoaTinhThanh(strInput, soNha, 0);
            }
            else // co ma dich vu cu the
            {
                if (0 == strInput.Length)
                {
                    SearchResultObject sr = new SearchResultObject();
                    sr.SoNha = soNha;
                    sr.MaDichVu = maDichVu;
                    this._lstSearchResult.Add(sr);
                }
                else
                {
                    this.SearchTuKhoaTinhThanh(strInput, soNha, maDichVu);
                }
            }
        }

        private void SearchTuKhoaTinhThanh(string strInput, string soNha, int maDichVu)
        {
            List<KeyCityDTO> lstTuKhoaTinhThanhDTO = this._tuKhoaTinhThanhDAO.Search(strInput);
            int nTuKhoaTinhThanhDTO = lstTuKhoaTinhThanhDTO.Count;
            if (nTuKhoaTinhThanhDTO > 0)
            {
                string strTemp = string.Empty;
                for (int i = 0; i < lstTuKhoaTinhThanhDTO.Count; i++)
                {
                    strTemp = Clear(strInput, lstTuKhoaTinhThanhDTO[i].KeyCity);
                    if (strTemp.Length > 0)
                    {
                        this.SearchTuKhoaQuanHuyen(strTemp, soNha, maDichVu, lstTuKhoaTinhThanhDTO[i].CityId);
                    }
                    else
                    {
                        SearchResultObject sr = new SearchResultObject();
                        sr.SoNha = soNha;
                        sr.MaDichVu = maDichVu;
                        sr.MaTinhThanh = lstTuKhoaTinhThanhDTO[i].CityId;
                        this._lstSearchResult.Add(sr);
                    }
                }
            }

            this.SearchTuKhoaQuanHuyen(strInput, soNha, maDichVu, 0);
        }

        private void SearchTuKhoaQuanHuyen(string strInput, string soNha, int maDichVu, int maTinhThanh)
        {
            List<KeyDistrictDTO> lstTuKhoaQuanHuyenDTO = this._tuKhoaQuanHuyenDAO.Search(strInput);
            int nTuKhoaQuanHuyenDTO = lstTuKhoaQuanHuyenDTO.Count;
            if (nTuKhoaQuanHuyenDTO > 0)
            {
                string strTemp = string.Empty;
                for (int i = 0; i < lstTuKhoaQuanHuyenDTO.Count; i++)
                {
                    strTemp = Clear(strInput, lstTuKhoaQuanHuyenDTO[i].KeyDistrict);
                    if (strTemp.Length > 0)
                    {
                        this.SearchTuKhoaPhuong(strTemp, soNha, maDichVu, maTinhThanh, lstTuKhoaQuanHuyenDTO[i].DistrictId);
                    }
                    else
                    {
                        SearchResultObject sr = new SearchResultObject();
                        sr.SoNha = soNha;
                        sr.MaDichVu = maDichVu;
                        sr.MaTinhThanh = maTinhThanh;
                        sr.MaQuanHuyen = lstTuKhoaQuanHuyenDTO[i].DistrictId;
                        this._lstSearchResult.Add(sr);
                    }
                }
            }

            this.SearchTuKhoaPhuong(strInput, soNha, maDichVu, maTinhThanh, 0);
        }

        private void SearchTuKhoaPhuong(string strInput, string soNha, int maDichVu, int maTinhThanh, int maQuanHuyen)
        {
            List<KeyWardDTO> lstTuKhoaPhuongDTO = this._tuKhoaPhuongDAO.Search(strInput);
            int nTuKhoaPhuongDTO = lstTuKhoaPhuongDTO.Count;
            if (nTuKhoaPhuongDTO > 0)
            {
                string strTemp = string.Empty;
                for (int i = 0; i < lstTuKhoaPhuongDTO.Count; i++)
                {
                    strTemp = Clear(strInput, lstTuKhoaPhuongDTO[i].KeyWard);
                    if (strTemp.Length > 0)
                    {
                        this.SearchTuKhoaDuong(strTemp, soNha, maDichVu, maTinhThanh, maQuanHuyen, lstTuKhoaPhuongDTO[i].WardId);
                    }
                    else
                    {
                        SearchResultObject sr = new SearchResultObject();
                        sr.SoNha = soNha;
                        sr.MaDichVu = maDichVu;
                        sr.MaTinhThanh = maTinhThanh;
                        sr.MaQuanHuyen = maQuanHuyen;
                        sr.MaPhuong = lstTuKhoaPhuongDTO[i].WardId;
                        this._lstSearchResult.Add(sr);
                    }
                }
            }

            this.SearchTuKhoaDuong(strInput, soNha, maDichVu, maTinhThanh, maQuanHuyen, 0);
        }

        private void SearchTuKhoaDuong(string strInput, string soNha, int maDichVu, int maTinhThanh, int maQuanHuyen, int maPhuong)
        {
            List<KeyStreetDTO> lstTuKhoaDuongDTO = this._tuKhoaDuongDAO.Search(strInput);
            int nTuKhoaDuongDTO = lstTuKhoaDuongDTO.Count;
            if (nTuKhoaDuongDTO > 0)
            {
                string strTemp = string.Empty;
                for (int i = 0; i < lstTuKhoaDuongDTO.Count; i++)
                {
                    strTemp = Clear(strInput, lstTuKhoaDuongDTO[i].KeyStreet);
                    if (strTemp.Length > 0)
                    {
                        this.SearchTuKhoaDiaDiem(strTemp, soNha, maDichVu, maTinhThanh, maQuanHuyen, maPhuong, lstTuKhoaDuongDTO[i].StreetId);
                    }
                    else
                    {
                        SearchResultObject sr = new SearchResultObject();
                        sr.SoNha = soNha;
                        sr.MaDichVu = maDichVu;
                        sr.MaTinhThanh = maTinhThanh;
                        sr.MaQuanHuyen = maQuanHuyen;
                        sr.MaPhuong = maPhuong;
                        sr.MaDuong = lstTuKhoaDuongDTO[i].StreetId;
                        this._lstSearchResult.Add(sr);
                    }
                }
            }

            this.SearchTuKhoaDiaDiem(strInput, soNha, maDichVu, maTinhThanh, maQuanHuyen, maPhuong, 0);
        }

        private void SearchTuKhoaDiaDiem(string strInput, string soNha, int maDichVu, int maTinhThanh, int maQuanHuyen, int maPhuong, int maDuong)
        {
            List<KeyPlaceNameDTO> lstTuKhoaDiaDiemDTO = this._tuKhoaDiaDiemDAO.Search(strInput);
            int nTuKhoaDiaDiemDTO = lstTuKhoaDiaDiemDTO.Count;
            if (nTuKhoaDiaDiemDTO > 0)
            {
                string strTemp = string.Empty;
                for (int i = 0; i < lstTuKhoaDiaDiemDTO.Count; i++)
                {
                    strTemp = Clear(strInput, lstTuKhoaDiaDiemDTO[i].KeyPlaceName);
                    if (0 == strTemp.Length)
                    {
                        SearchResultObject sr = new SearchResultObject();
                        sr.SoNha = soNha;
                        sr.MaDichVu = maDichVu;
                        sr.MaTinhThanh = maTinhThanh;
                        sr.MaQuanHuyen = maQuanHuyen;
                        sr.MaPhuong = maPhuong;
                        sr.MaDuong = maDuong;
                        sr.MaTenDiaDiem = lstTuKhoaDiaDiemDTO[i].PlaceNameId;
                        this._lstSearchResult.Add(sr);
                    }
                }
            }
        }

        /// <summary>
        /// This function get information after filter
        /// </summary>
        /// <param name="lstDuLieuDTO">Source for filter</param>
        /// <param name="type">Type of filter</param>
        /// <returns>List of string in source which satify filter condition</returns>
        public List<string> GetFilterInfo(List<DataDTO> lstDuLieuDTO, int type)
        {
            List<string> lstInfo = new List<string>();
            List<string> result = new List<string>();

            if (type == _FILTER_INFO_DUONG)
            {
                for (int i = 0; i < lstDuLieuDTO.Count; i++)
                {
                    if (0 != lstDuLieuDTO[i].StreetId)
                    {
                        for (int j = 0; j < this._listAllStreet.Count; j++)
                        {
                            if (this._listAllStreet[j].Id == lstDuLieuDTO[i].StreetId)
                            {
                                lstInfo.Add(this._listAllStreet[j].Name);
                                j = this._listAllStreet.Count + 1;
                            }
                        }
                    }
                }
                
                lstInfo.Sort();
                lstInfo.Insert(0, _TAT_CA_DUONG);
            }
            else if (type == _FILTER_INFO_DICHVU)
            {
                for (int i = 0; i < lstDuLieuDTO.Count; i++)
                {
                    if (0 != lstDuLieuDTO[i].ServiceId)
                    {
                        for (int j = 0; j < this._listAllService.Count; j++)
                        {
                            if (this._listAllService[j].Id == lstDuLieuDTO[i].ServiceId)
                            {
                                lstInfo.Add(this._listAllService[j].Name);
                                j = this._listAllService.Count + 1;
                            }
                        }
                    }
                }
                
                lstInfo.Sort();
                lstInfo.Insert(0, _TAT_CA_DICHVU);
            }

            else if (type == _FILTER_INFO_GIA)
            {
                _ListDetailServiceDTO = this.GetListDetaiServiceDTO(lstDuLieuDTO);
                for (int i = 0; i < _ListDetailServiceDTO.Count; i++)
                {
                    if (_ListDetailServiceDTO[i] != null)
                    {
                        lstInfo.Add(_ListDetailServiceDTO[i].Cost.ToString());
                    }
                }
                
                lstInfo = this.SortIntAsc(lstInfo);
                lstInfo.Insert(0, _TAT_CA_GIA);
            }

            result = this.RemoveDuplicate(lstInfo);
            
            return result;
        }

        private List<string> SortIntAsc(List<string> lstInfo)
        {
            for(int i = 0; i < lstInfo.Count; i++)
            {
                for(int j = i + 1; j < lstInfo.Count; j++)
                {
                    if(int.Parse(lstInfo[i]) > int.Parse(lstInfo[j]))
                    {
                        string temp = lstInfo[i];
                        lstInfo[i] = lstInfo[j];
                        lstInfo[j] = temp;
                    }
                }
            }
            return lstInfo;
        }

       

        private List<string> RemoveDuplicate(List<string> lstInfo)
        {
            List<string> result = new List<string>();
            if (lstInfo.Count > 0)
            {
                string _old = lstInfo[0];

                result.Add(_old);
                for (int i = 0; i < lstInfo.Count; i++)
                {
                    if (lstInfo[i] != _old)
                    {
                        _old = lstInfo[i];
                        result.Add(_old);
                    }
                }
            }

            return result;
        }

        private List<HardDataDTO> ConvertToHardData(List<DataDTO> result)
        {
            List<HardDataDTO> listHard = new List<HardDataDTO>();
            HardDataDTO temp = null;
            for (int i = 0; i < result.Count; i++)
            {
                temp = new HardDataDTO();
                temp.ServiceName = this.GetServiceNameFromID(result[i].ServiceId);
                temp.PlaceName = this.GetPlaceNameFromID(result[i].PlaceNameId);
                temp.NumberOfHome = result[i].NumberOfHome;
                temp.StreetName = this.GetStreetNameFromID(result[i].StreetId);
                temp.WardName = this.GetWardNameFromID(result[i].WardId);
                temp.DistrictName = this.GetDistrictNameFromID(result[i].DistrictId);
                temp.CityName = this.GetCityNameFromID(result[i].CityId);
                temp.Longitude = result[i].Longitude;
                temp.Latitude = result[i].Latitude;
                temp.Comment = result[i].Comment;
                listHard.Add(temp);
            }

            return listHard;
        }

        private string GetCityNameFromID(int p)
        {
            string result = string.Empty;
            for (int i = 0; i < this._listAllCity.Count; i++)
            {
                if (p == this._listAllCity[i].Id)
                {
                    result = this._listAllCity[i].Name;
                    return result;
                }
            }

            return result;
        }

        private string GetDistrictNameFromID(int p)
        {
            string result = string.Empty;
            for (int i = 0; i < this._listAllDistrict.Count; i++)
            {
                if (p == this._listAllDistrict[i].Id)
                {
                    result = this._listAllDistrict[i].Name;
                    return result;
                }
            }

            return result;
        }

        private string GetWardNameFromID(int p)
        {
            string result = string.Empty;
            for (int i = 0; i < this._listAllWard.Count; i++)
            {
                if (p == this._listAllWard[i].Id)
                {
                    result = this._listAllWard[i].Name;
                    return result;
                }
            }

            return result;
        }

        private string GetStreetNameFromID(int p)
        {
            string result = string.Empty;
            for (int i = 0; i < this._listAllStreet.Count; i++)
            {
                if (p == this._listAllStreet[i].Id)
                {
                    result = this._listAllStreet[i].Name;
                    return result;
                }
            }

            return result;
        }

        private string GetPlaceNameFromID(int p)
        {
            string result = string.Empty;
            for (int i = 0; i < this._listAllPlaceName.Count; i++)
            {
                if (p == this._listAllPlaceName[i].Id)
                {
                    result = this._listAllPlaceName[i].Name;
                    return result;
                }
            }

            return result;
        }

        private string GetServiceNameFromID(int p)
        {
            string result = string.Empty;
            for (int i = 0; i < this._listAllService.Count; i++)
            {
                if (p == this._listAllService[i].Id)
                {
                    result = this._listAllService[i].Name;
                    return result;
                }
            }

            return result;
        }

        private List<DataDTO> RemoveDupicatesOnSoNha(List<DataDTO> lstDuLieuDTO)
        {
            List<DataDTO> lstFinal = new List<DataDTO>();

            try
            {
                Dictionary<string, int> uniqueStore = new Dictionary<string, int>();

                string soNha = string.Empty;
                foreach (DataDTO duLieuDTO in lstDuLieuDTO)
                {
                    soNha = duLieuDTO.NumberOfHome;
                    if (!uniqueStore.ContainsKey(soNha))
                    {
                        uniqueStore.Add(soNha, 0);
                        lstFinal.Add(duLieuDTO);
                    }
                }
            }
            catch (Exception ex)
            {
                lstFinal = new List<DataDTO>();
                ex.ToString();
            }

            return lstFinal;
        }

        /// <summary>
        /// Clear string
        /// </summary>
        /// <param name="strInput">String to clear</param>
        /// <param name="strClear">String will be clear</param>
        /// <returns>String after clear</returns>
        public string Clear(string strInput, string strClear)
        {
            string strOutput = strInput;
            if (null != strClear && strClear.Length > 0)
            {
                int idx = strClear.IndexOf(strInput);
                if (idx >= 0)
                {
                    strOutput = string.Empty;
                }
                else
                {
                    strOutput = strInput.Replace(strClear, string.Empty);
                    strOutput = strOutput.Trim();
                }
            }

            return strOutput;
        }

        public List<String> GetAutoCorrect(string input)
        {
            List<String> result = new List<String>();

            if (_DictionaryCity.ContainsKey(input))
                result.Add(_DictionaryCity[input]);

            if (_DictionaryDistrict.ContainsKey(input))
                result.Add(_DictionaryDistrict[input]);

            if (_DictionaryPlaceName.ContainsKey(input))
                result.Add(_DictionaryPlaceName[input]);

            if (_DictionaryService.ContainsKey(input))
                result.Add(_DictionaryService[input]);

            if (_DictionaryStreet.ContainsKey(input))
                result.Add(_DictionaryStreet[input]);

            if (_DictionaryWard.ContainsKey(input))
                result.Add(_DictionaryWard[input]);

            return result;
        }

        private double HESOMAP = 0.001;

        public List<DataDTO> SearchNearby(DataDTO input)
        {
            List<DataDTO> result = new List<DataDTO>();

            List<DataDTO> allObject = this._duLieuDAO.SearchWithMaDichVu(input.ServiceId);

           foreach(DataDTO data in allObject)
           {
               if(CheckLongLati(data.Latitude, input.Latitude) && CheckLongLati(data.Longitude, input.Longitude))
               {
                   result.Add(data);
               }
           }

            return result;
        }

        private bool CheckLongLati(double input, double willCompare)
        {
            if (input <= (willCompare + HESOMAP) && input >= (willCompare - HESOMAP))
                return true;
            return false;
        }
    }
}
